﻿

using IotClientService.Models.Lg;
using IotClientService.Models.Lg.Remote;
using IotClientService.Services;
using IotContract.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XT.Common.Extensions;

namespace IotClientService.Handles.lgs;
/// <summary>
/// 清洗机
/// </summary>
public class LgHandleClear:LgHandleBase
{
    public LgHandleClear(ILgApiService lgCallApiService) : base(lgCallApiService)
    {
    }

    /// <summary>
    /// 设备类型
    /// </summary>
    public override IotEquipType EquipType { get; set; } = IotEquipType.CLEAR;
    /// <summary>
    /// 处理消息
    /// </summary>
    /// <param name="msg"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public override async Task<bool> HandleMsg(LgMsgInfo msg)
    {
        string replyCmd = string.Empty;
        switch (msg.Command)
        {
            // 作业实处理Data报告
            case "901":
                replyCmd = "902";
               await Handle901(msg, replyCmd);
                break;

            // 设备状态报告指示回应
            case "910":
               await Handle910(msg, "910");
                // send 833 

                break;
            // 设备状态报告
            case "913":
               await Handle913(msg, "913");

                break;
            // 作业mode报告
            case "917":
                replyCmd = "918";
               await Handle917(msg, replyCmd);
                break;
            // 通信状态报告邀请
            case "925":
                replyCmd = "926";
               await Handle925(msg, replyCmd);
                break;
            // 通信状态报告回应
            case "926":
               await Handle926(msg, "926");
                break;
            // 电芯投入报告
            case "933":
                replyCmd = "934";
               await Handle933(msg, replyCmd);
                break;

            // 未知命令,错误
            case "981":
               await Handle981(msg, "981");
                break;
            // 设备Trouble报告
            case "985":
               await Handle985(msg, "985");
                break;


            // 设置时间指示
            case "999":
               await Handle999(msg, "999");
                break;
            default:
                // send 981,
               await Handle981(msg, "981");
                break;
        }
        return true;
    }
    /// <summary>
    /// 作业实处理Data报告
    /// </summary>
    /// <param name="msg"></param>
    /// <param name="replyCmd"></param>
    private async Task Handle901(LgMsgInfo msg, string replyCmd)
    {
        var sendMsg = new LgMsgInfo
        {
            Direction = 0,
            ObjID = msg.ObjID,
            HasMsg = 0,
            SeqNo = msg.SeqNo,
            Command = replyCmd,

        };

        // 1 cellid  24
        string cellId = msg.Message.Substring(18, 24);
        // ng判定  0:ok 1:ng
        string ngCharge = msg.Message.Substring(42, 1);
        // ng code
        string ngCode = msg.Message.Substring(43, 4);
        // 结果数据
        string result = msg.Message.Substring(47, 56);

        LgClearResult clearResult = new LgClearResult
        {
            StartTime = result.Substring(0, 14),
            EndTime = result.Substring(14, 14),
            Station = result.Substring(28, 1).To<int>(),
            CellSurfaceTemperature = result.Substring(29, 5).To<double>(),
            CellCleaningTime = result.Substring(34, 4).To<double>(),
            CellDryingTime = result.Substring(38, 4).To<double>(),
            SteamNozzlePressure = result.Substring(42, 4).To<double>(),
            SteamNozzleFlow = result.Substring(46, 5).To<double>(),
            SteamNozzleTemperature = result.Substring(51, 5).To<double>()
        };

       var endResult= await Api.CommitClearResult(new LgClearDataRequest
        {
            Cell = cellId,
            IsNg = ngCharge.To<int>(),
            NgCode = ngCode,
            Result = clearResult,
            EquipNum = Equip.Num,
            EquipType = this.EquipType,
            ProgramType = Equip.ProgramType
        });
        // 0:ok 1:ng
        var ret = 0;
        if (!endResult.Success)
        {
            Equip.ExMessage = endResult.Message;
            ret = 1;
        }
        else
        {
            ret = endResult.Result?.Ret ?? 1;
        }
        if (ret == 1)
        {
            msg.Body = ret.ToString();
            await Handle981(msg, "981");
            return;
        }



        // 24长度
        var cellid = cellId.PadRight(24, ' ');
       
        var ng = endResult.Result.NgCodes?.FirstOrDefault() ?? "0000";

        sendMsg.Body = $"{cellid}{ret}{ng}";
        sendMsg.IsSend = true;
        InvokeSend(sendMsg);


    }


  
   
   
  
    /// <summary>
    /// 电芯投入报告
    /// </summary>
    /// <param name="msg"></param>
    /// <param name="replyCmd"></param>
    private async Task Handle933(LgMsgInfo msg, string replyCmd)
    {
        var sendMsg = new LgMsgInfo
        {
            Direction = 0,
            ObjID = msg.ObjID,
            HasMsg = 0,
            SeqNo = msg.SeqNo,
            Command = replyCmd,

        };
        // 托盘
        var cellid = msg.Message.Substring(18, 24);

       
        Equip.Obj = cellid;

        Equip.Remark = $"cell load:{cellid}";


        var result= await Api.CommitClearEnter(new Models.Lg.Remote.LgCellEnterRequest
        {
            Cell = cellid,
            EquipNum = Equip.Num,
            EquipType = this.EquipType,
            ProgramType = Equip.ProgramType
        });
        // 0:ok 1:ng 
        int ng = 0;
        if (!result.Success)
        {
            Equip.ExMessage = result.Message;
            ng = 1;

        }
        else
        {
            ng = result.Result?.Ret ?? 1;
        }
        if (ng == 1)
        {
            msg.Body = ng.ToString();
            await Handle981(msg, "981");
            return;
        }
        #region 传输body

        var cell = cellid.PadRight(24,' ');
        
        var ngCode = result.Result?.NgCodes?.FirstOrDefault() ?? "0000"; 

        #endregion
        sendMsg.Body = $"{cell}{ng}{ngCode}";
        sendMsg.IsSend = true;
        InvokeSend(sendMsg);
    }

   


}
